package com.ruoyi.module.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.module.domain.Account;
import com.ruoyi.module.domain.FundDetails;
import com.ruoyi.module.mapper.AccountMapper;
import com.ruoyi.module.mapper.FundDetailsMapper;
import com.ruoyi.module.service.IAccountService;
import com.ruoyi.module.service.IFundDetailsService;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.module.mapper.RechargeMapper;
import com.ruoyi.module.domain.Recharge;
import com.ruoyi.module.service.IRechargeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 在线充值Service业务层处理
 *
 * @author fubojin
 * @date 2025-01-10
 */
@Service
public class RechargeServiceImpl implements IRechargeService {
    @Autowired
    private RechargeMapper rechargeMapper;

    @Autowired
    private IAccountService accountService;

    @Autowired
    private IFundDetailsService fundDetailsService;

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询在线充值
     *
     * @param id 在线充值主键
     * @return 在线充值
     */
    @Override
    public Recharge selectRechargeById(Long id) {
        return rechargeMapper.selectRechargeById(id);
    }

    /**
     * 查询在线充值列表
     *
     * @param recharge 在线充值
     * @return 在线充值
     */
    @Override
    public List<Recharge> selectRechargeList(Recharge recharge) {
        return rechargeMapper.selectRechargeList(recharge);
    }

    /**
     * 新增在线充值
     *
     * @param recharge 在线充值
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertRecharge(Recharge recharge) {
        recharge.setRechargeNumber(OrderUtils.getOrderNo("CZ"));
        recharge.setCreateTime(DateUtils.getNowDate());
        recharge.setRechargeStatus(1);//申请
        return rechargeMapper.insertRecharge(recharge);
    }

    /**
     * 修改在线充值
     *
     * @param recharge 在线充值
     * @return 结果
     */
    @Override
    public int updateRecharge(Recharge recharge) {
        return rechargeMapper.updateRecharge(recharge);
    }

    @Override
    public int approve(Long id, boolean doublingSwitch,  Recharge rechargePo) {

        Recharge recharge = rechargeMapper.selectRechargeById(id);
        recharge.setRechargeStatus(2);
        recharge.setUpdateTime(DateUtils.getNowDate());
        rechargeMapper.updateRecharge(recharge);

        //个人账户余额处理
        Account account = accountService.selectAccountByUserId(recharge.getUserId());
        BigDecimal price = recharge.getPrice();
        if (doublingSwitch) {
            price = price.add(price);
        }
        account.setAccountBalance(account.getAccountBalance().add(price));
        accountService.updateAccount(account);

        //资金处理
        FundDetails fundDetails = new FundDetails();
        SysDept sysDept = deptMapper.selectDeptById(recharge.getDeptId());
        fundDetails.setUserId(sysDept.getUserId());
        fundDetails.setDeptId(recharge.getDeptId());
        fundDetails.setDeptName(sysDept.getDeptName());

        fundDetails.setOrderNo(recharge.getRechargeNumber());
        fundDetails.setFundType(3);
        if (doublingSwitch) {
            fundDetails.setLoanType(2);
        }else {
            fundDetails.setLoanType(1);
        }
        fundDetails.setVariableAmount(recharge.getPrice());
        fundDetailsService.insertFundDetails(fundDetails);


        //添加翻倍
        if (doublingSwitch) {
            fundDetails.setId(null);
            fundDetails.setFundType(10);
            fundDetails.setLoanType(3);
            fundDetails.setRemark(rechargePo.getRemark());
            fundDetails.setLoanMoney(new BigDecimal(0));
            fundDetailsService.insertFundDetails(fundDetails);
        }
        return 1;
    }


    @Override
    public int reject(Long id) {
        Recharge recharge = rechargeMapper.selectRechargeById(id);
        recharge.setRechargeStatus(3);
        recharge.setUpdateTime(DateUtils.getNowDate());
        rechargeMapper.updateRecharge(recharge);
        return 1;
    }

    /**
     * 批量删除在线充值
     *
     * @param ids 需要删除的在线充值主键
     * @return 结果
     */
    @Override
    public int deleteRechargeByIds(Long[] ids) {
        return rechargeMapper.deleteRechargeByIds(ids);
    }

    /**
     * 删除在线充值信息
     *
     * @param id 在线充值主键
     * @return 结果
     */
    @Override
    public int deleteRechargeById(Long id) {
        return rechargeMapper.deleteRechargeById(id);
    }
}
